package com.example.androidmainshixun.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.adapter.CartBean;
import com.example.androidmainshixun.adapter.CartAdapter;


import java.util.ArrayList;
import java.util.List;

public class CartActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private CartAdapter cartAdapter;
    private final List<CartBean> cartBeans=new ArrayList<>();
    private TextView total,edit;
    private Button pay;
    private CheckBox allSelect;
    private boolean isEditing=false;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        recyclerView=findViewById(R.id.recyclerView);
        total=findViewById(R.id.total);
        edit=findViewById(R.id.edit);
        pay=findViewById(R.id.pay);
        allSelect=findViewById(R.id.allSelect);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        cartAdapter=new CartAdapter(cartBeans,this);
        recyclerView.setAdapter(cartAdapter);

        cartAdapter.setOnChange(new CartAdapter.OnChange() {
            @Override
            public void change(List<CartBean> selects) {
                System.out.println(isEditing);
                if (selects.size()!=0){
                    if (isEditing){
                        pay.setText("删除（"+selects.size()+")");
                    }
                    else {
                        pay.setText("结算（"+selects.size()+")");

                    }
                }
                else {
                    if (isEditing){
                        pay.setText("删除");
                    }
                    else {
                        pay.setText("结算");
                    }
                }
                double price=0;
                for (CartBean cartBean:selects){
                    price+=cartBean.getPrice()*cartBean.getNumber();
                }
                total.setText(String.format("合计%1$.2f￥", price));
            }
        });
        getData();
        init();
    }
    private void getData(){
        cartBeans.clear();
        cartBeans.add(new CartBean("橙子",R.drawable.f1 ,false,1,9.24));
        cartBeans.add(new CartBean("香蕉",R.drawable.f3 ,false,1,8.3));
        cartBeans.add(new CartBean("水蜜桃",R.drawable.f5 ,false,1,8.72));
        cartBeans.add(new CartBean("蓝莓",R.drawable.f2 ,false,1,12.6));
        cartAdapter.notifyDataSetChanged();
    }

    private void init(){
        allSelect.setOnCheckedChangeListener((compoundButton, b) -> {
            for (CartBean cartBean:cartBeans){
                cartBean.setSelect(b);
            }
            cartAdapter.notifyDataSetChanged();
        });
        pay.setOnClickListener(view -> {
            if (isEditing){
                if (cartAdapter.getSelects()!=null&&cartAdapter.getSelects().size()!=0){
                    for (CartBean cartBean:cartAdapter.getSelects()){
                        cartBeans.remove(cartBean);
                    }
                    cartAdapter.notifyDataSetChanged();
                    pay.setText("删除");
                    Toast.makeText(CartActivity.this,"已删除",Toast.LENGTH_SHORT).show();
                    cartAdapter.getSelects().clear();
                }
                else {
                    Toast.makeText(CartActivity.this, "没有选择商品", Toast.LENGTH_SHORT).show();
                }
            }
            else {
                if (cartAdapter.getSelects()!=null&&cartAdapter.getSelects().size()!=0){
                    for (CartBean cartBean:cartAdapter.getSelects()){
                        cartBeans.remove(cartBean);
                    }
                    cartAdapter.notifyDataSetChanged();
                    cartAdapter.getSelects().clear();
                    pay.setText("结算");
                    total.setText("合计0.00￥");
                    allSelect.setChecked(false);
                    Toast.makeText(CartActivity.this, "结算成功", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(CartActivity.this, "没有选择商品", Toast.LENGTH_SHORT).show();
                }
            }
        });

        edit.setOnClickListener(view -> {
            if (!isEditing){
                edit.setText("取消编辑");
                if (cartAdapter.getSelects().size()!=0){
                    pay.setText("删除（"+cartAdapter.getSelects().size()+")");
                }
                else {
                    pay.setText("删除");
                }
            }
            else {
                edit.setText("编辑");
                if (cartAdapter.getSelects().size()!=0){
                    pay.setText("结算（"+cartAdapter.getSelects().size()+")");
                }
                else {
                    pay.setText("结算");
                }
            }
            isEditing=!isEditing;
        });
    }
}